/*!
 * 分页.
 * ecp.qzz.ui.paged.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.icombobox","qzz.window"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui");
	/**
	 * 视图控件.
	 */
	qzz.ui.paged = qzz.Class(qzz.ui.window, {
		_create:function() {
			this.getOption().width = this.getOption().width || 620;
			this.getOption().height = this._getNumberValue(this.getOption().height, 25);
			this.base();
			this.getOption().pageSize = this._getNumberValue(this.getOption().pageSize, 10);
			//添加微小版属性
			this.getOption().minPager = this.getBooleanValue(this.getOption().minPager, false);
			this.getOption().pageIndex = this._getNumberValue(this.getOption().pageIndex, 1);
			this.getOption().border = this._getNumberValue(this.getOption().border, 0);
			if(this.getOption().pageSizeList == null) {
			    this.getOption().pageSizeList = [10,25,50,100,200];
			}
			
			this._totalCount = this._getNumberValue(this.getOption().totalCount, 0);			
			this._pageCount = parseInt(this._totalCount/this.getOption().pageSize, 10);
		    if(this._totalCount/this.getOption().pageSize - this._pageCount > 0) {
		    	this._pageCount++;
		    }
			this._priorPage = {pageSize:10,pageIndex:1};
		},
		_render:function() {
			this.base();
			this._pageTab = this.createElement("TABLE");
			this.thisWindow.appendChild(this._pageTab);
			this._pageTab.style.marginLeft = "auto";
			//设置宽度
			this.setStyle(this._pageTab, "pagerTable");
			var tr = this._pageTab.insertRow(0);
			var k = 0;
			var td = tr.insertCell(k++);
			td.setAttribute("id","common_td");
			this._setHtml(td, this.getI18n("qzz.recordCount", "共{0}条，").replace("{0}", this._totalCount), false);
			this.totalCountTd = td;
			//this.totalCountTd.style.width = "80px";
			this.totalCountTd.style.textAlign = "right";
			
			td = tr.insertCell(k++);
			td.setAttribute("id", "common_td");
			this._setHtml(td, this.getI18n("qzz.filterCount", "当前页筛选出{0}条，").replace("{0}", this._filterCount||"0"), false);
			td.style.textAlign = "right";
			this.filterCountTd = td;
			
			td = tr.insertCell(k++);
			td.setAttribute("id","common_td");
			//用一个窗口装pagesize
			td.style.width = "165px";
			this._pageSizeTd = td;
			this._pageSizeTab = this.createElement("TABLE");
			this.setStyle(this._pageSizeTab, "pagerTable");
			td.appendChild(this._pageSizeTab);
			var psk = 0;
			var pstr = this._pageSizeTab.insertRow(0);
			var pstd = pstr.insertCell(psk++);
			this._setHtml(pstd, this.getI18n("qzz.pershow", "每页显示") + "&nbsp;", false);
			this._pageSizeListTd = pstr.insertCell(psk++);
			this._pageSizeListTd.setAttribute("id","common_td");
			this._pageSizeListTd.style.width = "56px";
			this.pageSizeList = new qzz.ui.input.combobox(null, {fontSize:12, data:this.getOption().pageSizeList, width:55, height:24, disInput:true});
			this.pageSizeList.setParent(this._pageSizeListTd);
			this.pageSizeList.setValue(this.getOption().pageSize);
			this.pageSizeList.setReadOnly(true);
			var _this = this;
			this.pageSizeList.bind("onSelect", function(index, item) {
				_this.setPageSize(item.text);
			});
			pstd = pstr.insertCell(psk++);
			pstd.setAttribute("id","common_td");
			this._setHtml(pstd, "&nbsp;" + this.getI18n("qzz.percount", "条记录"), false);
			
			
			td = tr.insertCell(k++);
			td.setAttribute("id","common_td");
			td.style.width = "1px";
			var indexTd = tr.insertCell(k++);
			indexTd.setAttribute("id","common_td");
			this._indexTab = this.createElement("TABLE");
			indexTd.appendChild(this._indexTab);
			this.setStyle(this._indexTab, "page_index_table");
			
			this._refreshIndex();
		},
		setPageSize:function(pageSize) {	
			this._priorPage.pageSize = this.getOption().pageSize;
			this._priorPage.pageIndex = this.getOption().pageIndex;
			if(pageSize === this.getI18n("qzz.all", "全部")) {
				this._pageCount = 1;
				this.getOption().pageSize = pageSize;
			} else {
				this.getOption().pageSize = parseInt(pageSize, 10);
				var pss = this._totalCount/this.getOption().pageSize;
			    this._pageCount = parseInt(pss, 10);
			    if(pss - this._pageCount > 0) {
			    	this._pageCount++;
			    }
			}
			this.pageSizeList.setValue(this.getOption().pageSize);
			this.getOption().pageIndex = 1;
			this._refreshIndex();
		},
		/**
		 * 设置页码.
		 * @param index 页码序号
		 * @param triggerEvent 激活事件开关
		 */
		setPageIndex:function(index, triggerEvent) {
			if(index > 0 && index <= this._pageCount)
			this._priorPage.pageIndex = this.getOption().pageIndex;
			this.getOption().pageIndex = index;
			this._refreshIndex(triggerEvent !== false);
		},
		nextPage:function() {
			if(this.getOption().pageIndex < this._pageCount) {
				this._priorPage.pageIndex = this.getOption().pageIndex;
				this.getOption().pageIndex ++;
				this._refreshIndex();
			}
		},
		getPageIndex:function() {
			return this.getOption().pageIndex;
		},
		getPageCount:function() {
			return this._pageCount || 1;
		},
		getPageSize:function() {
			return this.getOption().pageSize;
		},
		priorPage:function() {
			if(this.getOption().pageIndex > 1) {
				this._priorPage.pageIndex = this.getOption().pageIndex;
				this.getOption().pageIndex --;
				this._refreshIndex();
			}
		},
		lastPage:function() {
			if(this.getOption().pageIndex < this._pageCount) {
				this._priorPage.pageIndex = this.getOption().pageIndex;
				this.getOption().pageIndex = this._pageCount;
				this._refreshIndex();
			}
		},
		firstPage:function(fireEvent) {
			if(fireEvent === true || this.getOption().pageIndex > 1) {
				this._priorPage.pageIndex = this.getOption().pageIndex;
				this.getOption().pageIndex = 1;
				this._refreshIndex(fireEvent);
			}		
		},
		reWidth:function(width) {
			if(width > 1) {
				this.setWidth(width);
				this._refreshIndex();
			}
		},
		_refreshIndex:function(triggerEvent) {		
			//var nextWidth = 28;
			while(this._indexTab.rows.length > 0) {
			    this._indexTab.deleteRow(0);
			}
			if(this._filterCount != null 
					&& this.getOption().filterCount === true 
						&& this.getOption().minPager !== true) {
				this.filterCountTd.style.display = "";
			    this._setHtml(this.filterCountTd, this.getI18n("qzz.filterCount", "当前页筛选出{0}条，").replace("{0}", this._filterCount), false);
			} else {
				this.filterCountTd.style.display = "none";
			}
			this._setHtml(this.totalCountTd, this.getI18n("qzz.recordCount", "共{0}条，").replace("{0}", this._totalCount), false);
			var tr = this._indexTab.insertRow(0);
			k = 0;
			var td = tr.insertCell(k++)
			var _this = this;
			this._setHtml(td, "&lt;&lt;", false);
			if(this.getOption().pageIndex == 1) {
				this.setStyle(td, "page_index_cell_disabled");
			} else {
			    this.setStyle(td, "page_index_cell");
			    td.onclick = function() {
			    	_this.firstPage();
			    }
			}
			//td.style.width = nextWidth + "px";
			td = tr.insertCell(k++);
			this._setHtml(td, "&lt;", false);		
			if(this.getOption().pageIndex == 1) {
				this.setStyle(td, "page_index_cell_disabled");
			} else {
			    this.setStyle(td, "page_index_cell");
			    td.onclick = function() {
					_this.priorPage();
				}
			}
			//td.style.width = nextWidth + "px";
			var sindex = this.getOption().pageIndex - 1;
			if(sindex < 1) sindex = 1;
			var eindex = sindex + 2;
			
			if(eindex > this._pageCount) {
				eindex = this._pageCount;
			}
			//限制序号输出
			if(this.getOption().minPager !== true) {
				if(sindex > this._pageCount - 4) {
					sindex = this._pageCount - 4;
					if(sindex < 1) {
						sindex = 1;
					}
				}
			}			
			if(eindex < sindex && sindex == 1) {
				eindex = 1;
			}
			for(var i = sindex; i <= eindex; i++) {
				td = tr.insertCell(k++);
				if(i === this.getOption().pageIndex) {
					this.setStyle(td, "page_index_cell active");
				} else {
				    this.setStyle(td, "page_index_cell");
				}
				this._setHtml(td, i, false);
				td.onclick = function() {
					_this.setPageIndex(parseInt(this.innerHTML,10));
				}
			}
			//隐藏部分序号输出
			if(this.getOption().minPager !== true) {
				if(this._pageCount == eindex + 2) {
					td = tr.insertCell(k++);
					if(eindex + 1 === this.getOption().pageIndex) {
						this.setStyle(td, "page_index_cell active");
					} else {
					    this.setStyle(td, "page_index_cell");
					}
					this._setHtml(td, eindex + 1, false);
					td.onclick = function() {
						_this.setPageIndex(parseInt(this.innerHTML,10));
					}
				} else if(this._pageCount > eindex + 2) {
					td = tr.insertCell(k++);
					this.setStyle(td, "three_point");
					this._setHtml(td, "...", false);		
					td.onclick = function() {
						_this.setPageIndex(_this.getOption().pageIndex + 3);
					}
				}
				if(this._pageCount >= eindex + 1) {
					td = tr.insertCell(k++);
					if(this._pageCount === this.getOption().pageIndex) {
						this.setStyle(td, "page_index_cell active");
					} else {
					    this.setStyle(td, "page_index_cell");
					}
					this._setHtml(td, this._pageCount, false);
					td.onclick = function() {
						_this.setPageIndex(parseInt(this.innerHTML,10));
					}
				}
			}
			td = tr.insertCell(k++);
			this._setHtml(td, "&gt;", false);
			if(this.getOption().pageIndex >= this._pageCount) {
				this.setStyle(td, "page_index_cell_disabled");
			} else {
			    this.setStyle(td, "page_index_cell");
			    td.onclick = function() {
					_this.nextPage();
				}
			}
			//td.style.width = nextWidth + "px";
			td = tr.insertCell(k++);
			this._setHtml(td, "&gt;&gt;", false);
			if(this.getOption().pageIndex >= this._pageCount) {
				this.setStyle(td, "page_index_cell_disabled");
			} else {
			    this.setStyle(td, "page_index_cell");
			    td.onclick = function() {
			    	_this.lastPage();
			    }
			}
			//分页页码宽度默认为40px
			var pageIndexWidth = 40;
			this._indexTab.style.maxWidth = (pageIndexWidth * k) + "px";
			
			//td.style.width = nextWidth + "px";
			if(this.hasbind("onPageChanged") && triggerEvent !== false) {
				var ps = this.getOption().pageSize;
				if(ps === this.getI18n("qzz.all", "全部")) {
					ps = this._totalCount;
				}
				this.trigger("onPageChanged",[this._pageCount, ps, this.getOption().pageIndex]);
			}
			//隐藏pageSize
			if(this.getOption().minPager === true) {
			    this._pageSizeTd.style.display = "none";
			    this._indexTab.parentElement.style.minWidth = "";
			} else {
				this._pageSizeTd.style.display = "";	
				this._indexTab.parentElement.style.minWidth = this._indexTab.style.maxWidth;
			}			
			this.thisWindow.style.marginLeft = "auto";
		},
		setFilterCount:function(count) {
			this._filterCount = count;
			this._refreshIndex(false);
		},
		clearFilterCount:function() {
			this._filterCount = null;
			this._refreshIndex(false);
		},
		setTotalCount:function(count, triggerEvent, curCount) {
			count = parseInt(count, 10);
			if(this._totalCount != count) {
				this._totalCount = count;
				if(this.getOption().pageSize === this.getI18n("qzz.all", "全部")) {
					this._pageCount = 1;
				} else {
					if(curCount == null) {
						curCount = this.getOption().pageSize;
					}
					//如果当前页多于pageSize则总明细减少多出来的部分.
					if(curCount > this.getOption().pageSize) {
						count -= curCount - this.getOption().pageSize;
					}
				    this._pageCount = parseInt(count/this.getOption().pageSize, 10);
				    if(count/this.getOption().pageSize - this._pageCount > 0) {
				    	this._pageCount++;
				    }
				}
			}
			if(triggerEvent !== false) {
				this._priorPage.pageIndex = 1;
				this.getOption().pageIndex = 1;
			}
			this._refreshIndex(triggerEvent);
			
		},		
		getTotalCount:function() {
			return this._totalCount;
		},
		setPageSizeList:function(psl) {
			if(this.isArray(psl)) {
				for(var i = 0, len = psl.length; i < len; i++) {
					if(psl[i] !== this.getI18n("qzz.all", "全部")) {
					    psl[i] = parseInt(psl[i], 10);
					}
				}
			    this.getOption().pageSizeList = psl;
			    if(psl.length > 0) {
			    	this._priorPage.pageSize = this.getOption().pageSize;
			    	this._priorPage.pageIndex = this.getOption().pageIndex;
			    	this.pageSizeList.setItemList(psl);
			    	this.getOption().pageSize = psl[0];
			    	this.pageSizeList.setValue(this.getOption().pageSize);
			    	this.getOption().pageIndex = 1;
			    }
			    this._refreshIndex();
			}
		},
		/**
		 * 还原上一次的分页状态.
		 */
		resetPageState:function() {
			this.getOption().pageSize = this._priorPage.pageSize;
			this.getOption().pageIndex = this._priorPage.pageIndex;
			this._refreshIndex();
		}
	});
	/**
	 * 列表控件.
	 */
	_bindRenderFunc("qzzpaged", qzz.ui.paged);
	return qzz.ui.paged;
});